package common.activiti.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Event;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import common.security.web.util.SecurityUtil;
import common.web.vo.Result;

/**
 */
@RestController
@RequestMapping("/bpm/comment")
public class CommentController {

	@Autowired
	TaskService taskService;

	@Autowired
	IdentityService identityService;

	@Autowired
	HistoryService historyService;

	/**
	 * 保存意见
	 */
	@RequestMapping(value = "save/{taskId}", method = RequestMethod.POST)
	public Result addComment(@PathVariable("taskId") String taskId, @RequestBody JSONObject json) {
		String processInstanceId = json.getString("processInstanceId");
		String message = json.getString("message");
		identityService.setAuthenticatedUserId(SecurityUtil.getUserName());
		taskService.addComment(taskId, processInstanceId, message);
		return Result.SUCCESS;
	}

	/**
	 * 读取意见
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public Result list(@RequestBody JSONObject json) {

		String processInstanceId = json.getString("processInstanceId");
		String taskId = json.getString("taskId");

		Map<String, Object> commentAndEventsMap = new HashMap<String, Object>();
		Result result = Result.createResult(null);

		/*
		 * 根据不同情况使用不同方式查询
		 */
		if (StringUtils.isNotBlank(processInstanceId)) {
			List<Comment> processInstanceComments = taskService.getProcessInstanceComments(processInstanceId);
			for (Comment comment : processInstanceComments) {
				String commentId = comment.getId();
				commentAndEventsMap.put(commentId, comment);
			}

			// 提取任务任务名称
			List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()
					.processInstanceId(processInstanceId).list();
			Map<String, String> taskNames = new HashMap<String, String>();
			for (HistoricTaskInstance historicTaskInstance : list) {
				taskNames.put(historicTaskInstance.getId(), historicTaskInstance.getName());
			}
			result.addMap("taskNames", taskNames);

		}

		/*
		 * 查询所有类型的事件
		 */
		if (StringUtils.isNotBlank(taskId)) { // 根据任务ID查询
			List<Event> taskEvents = taskService.getTaskEvents(taskId);
			for (Event event : taskEvents) {
				String eventId = event.getId();
				commentAndEventsMap.put(eventId, event);
			}
		}

		result.addMap("events", commentAndEventsMap.values());
		return result;
	}

}
